<?php

namespace App\Model\Setting;

use Hyperf\DbConnection\Model\Model;

/**
 *  临时配置
 */
class TmpSetting extends Model
{
    protected $table = 'tmp_setting';
    protected $fillable = ['setting_type', 'setting_value'];

    static function getExtraPayOn()
    {
        $item = self::where('setting_type', 'extra_pay_on')->first();
        return !$item ? 0 : intval($item->setting_value);
    }

    static function getExtraPayTotal()
    {
        $item = self::where('setting_type', 'extra_pay_total')->first();
        return !$item ? 1000 : intval($item->setting_value);
    }

    /**
     * 邀请奖励 - 开关
     */
    static function getRewardFilter()
    {
        $item = self::where('setting_type', 'reward_filter')->first();
        return !$item ? 0 : intval($item->setting_value);
    }

    /**
     * 邀请开关
     */
    static function switchRewardFilter($value = 0)
    {
        $item = self::where('setting_type', 'reward_filter')->first();
        return !$item ? 0 : $item->update(['setting_value' => $value]);
    }

    /**
     * 邀请奖励子条件 - 开关
     */
    static function getRewardSubFilter($filterNo = 0)
    {
        $item = self::where('setting_type', 'reward_sub_filter')->first();
        if (!$item) return 0;
        $filters = json_decode($item->setting_value, true);
        return $filters[$filterNo] ?? 0;
    }

    static function addRewardSubFilter($filterNo = 1, $value = 1)
    {
        $item = self::where('setting_type', 'reward_sub_filter')->first();
        if (!$item) {
            return self::create([
                'setting_type' => 'reward_sub_filter',
                'setting_value' => json_encode([$filterNo => $value])
            ]);
        }
        $filters = json_decode($item->setting_value, true);
        $filters[$filterNo] = $value;
        return $item->update(['setting_value' => json_encode($filters)]);
    }

    /**
     * 邀请奖励 - 超时天数
     */
    static function getRewardExpDay()
    {
        $item = self::where('setting_type', 'reward_exp_day')->first();
        return !$item ? 30 : intval($item->setting_value);
    }

    /**
     * 邀请奖励 - 按数量间隔
     */
    static function getRewardOffCount()
    {
        $item = self::where('setting_type', 'reward_off_count')->first();
        return !$item ? 10 : intval($item->setting_value);
    }

    /**
     * 邀请开关
     */
    static function switchRewardOffCount($value = 10)
    {
        $item = self::where('setting_type', 'reward_off_count')->first();
        return !$item ? 0 : $item->update(['setting_value' => $value]);
    }

    /**
     * 是否开启融云消息回调
     */
    static function getMsgFilter()
    {
        $item = self::where('setting_type', 'msg_filter')->first();
        return !$item ? 0 : intval($item->setting_value);
    }

    /**
     * 充值限制总额
     */
    static function getPayTotal()
    {
        $item = self::where('setting_type', 'pay_total')->first();
        return !$item ? 5000000 : intval($item->setting_value);
    }

    /**
     * 每天充值限制总额
     */
    static function getDayPayTotal()
    {
        $item = self::where('setting_type', 'day_pay_total')->first();
        return !$item ? 1000000 : intval($item->setting_value);
    }

    /**
     * extra阈值设置
     */
    static function getExtraRateValue()
    {
        $item = self::where('setting_type', 'ex_rate_value')->first();
        return !$item ? 0 : intval($item->setting_value);
    }

    /**
     * extra订单倍数
     */
    static function getExtraMultiple()
    {
        $item = self::where('setting_type', 'ex_multiple')->first();
        return !$item ? 2 : intval($item->setting_value);
    }
}
